#include <string.h>
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;

template <class T>
T add(T x,T y){
    return x + y;
}

//针对通用模板解决不了的类型的处理
//使用特化模板
//规范建议：定义特化模板时指明针对什么类型进行的特化
template <>
const char * add<const char *>(const char * p1,const char * p2){
    char * temp = new char[strlen(p1) + strlen(p2) + 1]();
    strcpy(temp,p1);
    strcat(temp,p2);
    return temp;
}


void test0(){
    int a1 = 2, a2 = 4;
    double b1 = 2.7, b2 = 4.6;
    string s1("hello");
    string s2(",world");

    cout << add(a1,a2) << endl;
    cout << add(b1,b2) << endl;
    cout << add(s1,s2) << endl;

    const char * pstr = "hello,";
    const char * pstr2 = "wangdao";
    /* cout << add<string>(pstr,pstr2) << endl; */

    /* cout << add(pstr,pstr2) << endl; */
    const char * res = add(pstr,pstr2);
    cout << res << endl;
    delete [] res;
}

int main(void){
    test0();
    return 0;
}
